<template>
  <a-card :bordered='false'>
    <div class='card-title'>{{ $route.meta.title }}</div>
    <div class='table-operator'>
      <a-input-search
        style='max-width: 300px; min-width: 150px;'
        v-model='queryParam.search'
        placeholder='请输入外勤姓名'
        @search='onSearch'
      />
      <span style='margin-left: 20px'>
        选择时间：
        <a-month-picker
          v-model='queryParam.year_month'
          placeholder='选择年月'
          @change='handleRefresh(true)'
        />
      </span>
    </div>
    <s-table
      ref='table'
      rowKey='store_user_id'
      :loading='isLoading'
      :columns='columns'
      :data='loadData'
      :pageSize='15'
    >
      <div slot='total_assembly' slot-scope='text, item'>
        <span style='color: red'>￥</span>{{ item.total_assembly }}
      </div>
      <div class='actions' slot='action' slot-scope='text, item'>
        <a v-action:list @click='handleCommission(item)'>提成明细</a>
      </div>
    </s-table>
    <CommissionModel ref='CommissionModel' />
  </a-card>
</template>

<script>
import * as Api from '@/api/store/user'
import { STable } from '@/components'
import CommissionModel from './CommissionModel.vue'
import moment from 'moment'

export default {
  name: 'Index',
  components: {
    STable,
    CommissionModel
  },
  data() {
    return {
      // 角色列表
      roleList: [],
      // 查询参数
      queryParam: {
        year_month: moment(),
        position: 2
      },
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: 'ID',
          dataIndex: 'store_user_id'
        },
        {
          title: '外勤姓名',
          dataIndex: 'real_name'
        },
        {
          title: '月份',
          dataIndex: 'year_month_str'
        },
        {
          title: '总提成',
          dataIndex: 'total_assembly',
          scopedSlots: { customRender: 'total_assembly' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.list({ ...param, ...this.queryParam })
          .then(response => {
            return response.data.list
          })
      }
    }
  },
  created() {
  },
  methods: {

    // 新增记录
    handleAdd() {
      this.$refs.AddForm.add()
    },

    // 编辑记录
    handleEdit(item) {
      this.$refs.EditForm.edit(item)
    },

    handleCommission(item) {
      this.$refs.CommissionModel.queryParam.store_user_id = item.store_user_id
      this.$refs.CommissionModel.queryParam.year_month = this.queryParam.year_month
      this.$refs.CommissionModel.open(item.position)
    },

    // 删除记录
    handleDelete(item) {
      const app = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk() {
          return Api.deleted({ userId: item['store_user_id'] })
            .then(result => {
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally(result => modal.destroy())
        }
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh(bool = false) {
      this.$refs.table.refresh(bool)
    },

    // 检索查询
    onSearch() {
      this.handleRefresh(true)
    }

  }
}
</script>
